<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>叩丁狼骡窝窝后台管理系统</title>
    <#include "../common/header.ftl" >
    <link rel="stylesheet" href="/js/treeview/bootstrap-treeview.min.css" type="text/css"/>
    <script type="text/javascript" src="/js/treeview/bootstrap-treeview.min.js"></script>
    <script>
        $(function () {
            $.get('/region/selectByParentId.do',{type:'tree'},function (data) {
                console.log(data);
                var continent = [{text: "全部地区", nodes:data}];
                $('#treeview').treeview({
                    color: "#428bca",
                    showTags: true,
                    data: continent,
                    lazyLoad:function (node) {
                        console.log(node.id);
                        $.get('/region/selectByParentId.do',{type:'tree',parentId:node.id},function (data2) {
                            $('#treeview').treeview("addNode",[data2,node]);
                        });
                    },

                    onNodeSelected: function(event, node) {
                        $.get('/region/selectByParentId.do',{parentId:node.id},function (data2) {
                            console.log(data2);
                            $('#regionTable tbody').empty();
                            $.each(data2,function (index, ele) {
                                var tr = $('#template tr').clone(true);//模板中的tr克隆
                                $(tr).find('td:nth-child(1)').html(index+1);
                                $(tr).find('td:nth-child(2)').html(ele.name);
                                $(tr).find('a').attr('data-json',ele.json);
                                var json = JSON.parse(ele.json);
                                //判断是否为推荐
                                if (json.state==1){
                                    console.log($(tr).find('a:last'));
                                    $(tr).find('.btn-hot').html('<span class="glyphicon glyphicon-star"></span>取消推荐');
                                    $(tr).find('.btn-hot').prop("class","btn btn-danger btn-xs btn-hot");
                                }
                                $('#regionTable tbody').append($(tr));
                            })
                        });

                    }
                });
            });
            //编辑和新增
            $('.btn-input').click(function () {
                $('#editForm')[0].reset();
                var json = $(this).data('json');
                var selected = $('#treeview').treeview('getSelected')[0];
                if (json){
                    $('input[name="id"]').val(json.id);
                    $('input[name="name"]').val(json.name);
                    $('#parentName').val(json.parentName);
                    $('input[name="parent.id"]').val(json.parentId);
                } else {
                    if (selected!=null){
                        $('#parentName').val(selected.text);
                        $('input[name="parent.id"]').val(selected.id);
                    }
                }
                $('#inputModal').modal("show");
            });
            //编辑、新增提交
            $('.btn-submit').click(function () {
                $('#editForm').ajaxSubmit(function (data) {
                    handleResult(data);
                });
            });
            //修改推荐状态
            $('.btn-hot').click(function () {
                var id = $(this).data('json').id;
                var state = $(this).data('json').state;
                $.messager.confirm("温馨提示","确认修改吗?",function () {
                    $.post('/region/changeState.do',{id:id, state: state^1},function (data) {
                        handleResult(data);
                    });
                })
            });

        });

    </script>
  
</head>
<body>
<table id="template" hidden>
    <tr>
        <td></td>
        <td></td>
        <td>
            <a class="btn btn-info btn-xs btn-input" >
                <span class="glyphicon glyphicon-pencil"></span>编辑
            </a>

            <a class="btn btn-success btn-xs btn-hot" >
                <span class="glyphicon glyphicon-star-empty"></span>设为推荐
            </a>
        </td>
    </tr>
</table>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
            <#assign menu="region"/>
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/region/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <a href="javascript:;" class="btn btn-success btn-input">添加</a>
            </form>

            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview"></div>
                </div>

                <div class="col-sm-8">
                    <table class="table table-striped table-hover" id="regionTable">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>

<#--编辑模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"
                                   placeholder="请输入地区名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName" readonly>
                            <input type="hidden" class="form-control" id="parentId" name="parent.id" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</body>
</html>